package org.icnmasjid.member.webapp.action;

import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.icnmasjid.member.Constants;
import org.icnmasjid.member.util.DesEncrypter;
import org.icnmasjid.member.webapp.action.BaseFormController;
import org.icnmasjid.member.model.Person;
import org.icnmasjid.member.model.PersonPayment;
import org.icnmasjid.member.model.PersonSubOrgRole;
import org.icnmasjid.member.model.Role;
import org.icnmasjid.member.model.SubOrg;
import org.icnmasjid.member.service.PersonManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.sun.mail.iap.Response;

public class PersonFormRestrictedController extends BasePersonFormController {
    public void setPersonManager(PersonManager personManager) {
        this.personManager = personManager;
    }
    public PersonFormRestrictedController() {
        setCommandName("person");
        setCommandClass(Person.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
    throws Exception {
        Person person = null;
        // check to see if this is an user who followed the email web link to come to this page
        String key = request.getParameter("passKey");
        if ( key != null ) {
        	if ( StringUtils.isNotBlank(key) ) {
        		String personId = new DesEncrypter(Constants.CRYPTO_KEY).decrypt(key);
        		if ( NumberUtils.isNumber(personId) ) {
        			person = personManager.getPerson(personId);
        		}
        	}
        }
        if ( person == null ) {
        	throw new InvalidKeyException("The key is invalid");
        }
        // update the person record each time the user views it if ther is no http referer
        if ( request.getHeader("Referer") == null || !request.getHeader("Referer").matches(".*icnmasjid\\.org.*") ) {
        	person.setUserViewed(person.getUserViewed()+1);
        	personManager.savePerson(person);
        }
        return person;
    }

    public ModelAndView onSubmit(HttpServletRequest request,
                                 HttpServletResponse response, Object command,
                                 BindException errors)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'onSubmit' method...");
        }
        Person person = (Person) command;
        Locale locale = request.getLocale();
    	person.setPrimaryPerson(Boolean.TRUE);
    	person.setUserUpdated(person.getUserUpdated()+1);
        personManager.savePerson(person);
        saveMessage(request, getText("person.updated", locale));
        return new ModelAndView("redirect:editPersonRestricted.html","passKey", request.getParameter("passKey"));
    }
}
